<template>
  <div class="box">
    <div class="scroll-view">
      <projectMsg :projectId="project_id" :hrefProject="true"></projectMsg>
      <div class="user-title-box">基础信息</div>
      <el-form :model="shkczlRuleForm" :rules="shkczlRules" ref="shkczlRuleForm" label-position="top">
        <el-form-item label="资料文件(.zip,.ppt,.xls,.pdf,.doc,.docx,.xlsx,.rar)" prop="survey_data">
          <div class="file-list-box">
            <el-upload multiple accept=".zip,.ppt,.xls,.pdf,.doc,.docx,.xlsx,.rar" class="avatar-uploader" action="#"
              :show-file-list="false" :auto-upload="false" list-type="picture-card" :on-change="(file, fileList) => {
                shkczlFileChange(file, fileList, 'survey_data');
              }
                ">
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <div class="file-item-box" v-for="(item, index) in shkczlRuleForm.survey_data" :key="index">
              <div class="el-icon-delete-box" @click="shkczlFileRemove(index, 'survey_data')">
                <i class="el-icon-error"></i>
              </div>
              <el-tooltip class="item" effect="dark" :content="item.name || item.file_name" placement="top">
                <img class="img" :src="$fileImgFunc(item)" />
              </el-tooltip>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input :rows="4" class="input-box" v-model="shkczlRuleForm.remark" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="shkczlSubmit('shkczlRuleForm')" :disabled="diaLoading"
          :loading="diaLoading">提交</el-button>
        <el-button @click="shkczlCancel('shkczlRuleForm')">关闭</el-button>
      </span>
    </div>
  </div>
</template>
<script>
import projectApi from "@/api/projectApi";
import projectMsg from "@/components/ProjectMsg";

export default {
  name: "shkczlCom",
  components: {
    projectMsg,
  },
  data() {
    var survey_data = (rule, value, callback) => {
      if (!value.length) {
        callback(new Error("请上传勘场资料文件"));
      } else {
        callback();
      }
    };
    return {
      shkczlRuleForm: {
        remark: "",
        survey_data: [],
      },
      shkczlRules: {
        survey_data: [
          { required: true, validator: survey_data, trigger: "change" },
        ],
      },
      diaLoading: false,
      project_id: "",
    };
  },
  computed: {
  },
  mounted() {
    this.project_id = this.$route.query.id;
  },
  methods: {
    // 上传勘场资料
    shkczlSubmit(formName) {
      let shkczlRuleForm = this.shkczlRuleForm;
      console.log(shkczlRuleForm, "shkczlRuleForm");
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let formdata = new FormData();
          formdata.append(
            "data",
            JSON.stringify({
              remark: shkczlRuleForm.remark,
              project_id: this.project_id,
            })
          );
          shkczlRuleForm.survey_data.forEach((item) => {
            formdata.append("survey_data[]", item.raw);
          });
          this.diaLoading = true;
          projectApi
            .uploadSurveyData(formdata)
            .then((res) => {
              console.log(res, "上传勘场资料");
              this.diaLoading = false;
              if (res.code == 200) {
                this.shkczlCancel();
              }
              this.$message({
                type: res.code == 200 ? "success" : "warning",
                message: res.code == 200 ? "处理成功" : res.message,
              });
            })
            .catch((err) => {
              this.diaLoading = false;
              this.$message({
                type: "warning",
                message: err.message,
              });
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    shkczlCancel(formName) {
      this.$goBackTab(this);
    },
    shkczlFileChange(file, list, type) {
      console.log(file, list, type);
      let shkczlRuleForm = this.shkczlRuleForm;
      shkczlRuleForm[type].push(file);
      this.shkczlRuleForm = shkczlRuleForm;
    },
    shkczlFileRemove(index, type) {
      console.log(index, type);
      let shkczlRuleForm = this.shkczlRuleForm;
      shkczlRuleForm[type].splice(index, 1);
      this.shkczlRuleForm = shkczlRuleForm;
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/projectOparet.scss";
@import "@/styles/addPage.scss";
</style>